<template>
    <div>
        <el-card class="card">
            <div class="title">
                <h2 style="text-align: center">部门管理</h2>
            </div>
        </el-card>
        <br />
        <el-card>
            <el-row>
                <el-col :span="4">
                    <el-input v-model="name"><template slot="prepend">部门名称：</template></el-input>
                </el-col>
                <el-col :span="1" style="margin-left: 5px">
                    <el-button type="primary" @click="search">查询</el-button>
                </el-col>
                <el-col :span="1" :offset="1">
                    <el-button type="info" style="margin-bottom: 5px" @click="addDialog">新增部门</el-button>
                </el-col>
            </el-row>
            <div>
                <el-table :data="tableData" border style="width: 100%">
                    <el-table-column prop="name" label="名称" header-align="center" align="center"> </el-table-column>
                    <el-table-column prop="managerId" label="负责人" header-align="center" align="center" :show-overflow-tooltip="true"></el-table-column>
                    <el-table-column prop="phone" label="联系电话" header-align="center" align="center"></el-table-column>
                    <el-table-column prop="mail" label="邮箱" header-align="center" align="center"></el-table-column>
                    <el-table-column prop="remarks" label="描述" header-align="center" align="center" :show-overflow-tooltip="true"></el-table-column>
                    <el-table-column prop="type" label="类型" header-align="center" align="center">
                        <template slot-scope="scope">
                            <el-tag v-if="scope.row.type === 1" size="small" type="success">职能部门</el-tag>
                            <el-tag v-else size="small" type="info">二级院部</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" header-align="center" align="center" width="200">
                        <template slot-scope="scope">
                            <el-button type="primary" @click="editDepartmentById(scope.row.id)">编辑</el-button>
                            <el-button type="danger" @click="deleteDepartmentById(scope.row.id)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <br />
                <!--分页部分-->
                <el-pagination
                        background
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page="pageInfo.pageNum"
                        :page-sizes="[10, 15, 20, 25]"
                        :page-size="10"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="pageInfo.total">
                </el-pagination>
            </div>
        </el-card>
        <!--新增部门对话框-->
        <el-dialog title="新增部门" :visible.sync="addDialogVisible" width="30%">
            <el-form :model="addDepartmentForm" label-width="80px">
                <el-form-item label="名称" style="width: 60%">
                    <el-input v-model="addDepartmentForm.name" placeholder="必填"></el-input>
                </el-form-item>
                <el-form-item label="负责人" style="width: 60%">
                    <el-input v-model="addDepartmentForm.managerId" placeholder="多个用英文逗号分隔"></el-input>
                </el-form-item>
                <el-form-item label="联系电话" style="width: 60%">
                    <el-input v-model="addDepartmentForm.phone"></el-input>
                </el-form-item>
                <el-form-item label="邮箱" style="width: 60%">
                    <el-input v-model="addDepartmentForm.mail"></el-input>
                </el-form-item>
                <el-form-item label="描述" style="width: 60%">
                    <el-input v-model="addDepartmentForm.remarks"></el-input>
                </el-form-item>
                <el-form-item label="类型" size="mini" prop="isWork">
                    <el-radio-group v-model="addDepartmentForm.type">
                        <el-radio :label="1">职能部门</el-radio>
                        <el-radio :label="2">二级院部</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button type="primary" @click="pushAddDepartment">保存</el-button>
                <el-button type="info" @click="addDialogVisible = false">取消</el-button>
            </span>
        </el-dialog>
        <!--编辑部门对话框-->
        <el-dialog title="编辑部门" :visible.sync="editDialogVisible" width="30%">
            <el-form :model="editDepartmentForm" label-width="80px">
                <el-form-item label="名称" style="width: 60%">
                    <el-input v-model="editDepartmentForm.name" placeholder="必填"></el-input>
                </el-form-item>
                <el-form-item label="负责人" style="width: 60%">
                    <el-input v-model="editDepartmentForm.managerId" placeholder="必填"></el-input>
                </el-form-item>
                <el-form-item label="联系电话" style="width: 60%">
                    <el-input v-model="editDepartmentForm.phone"></el-input>
                </el-form-item>
                <el-form-item label="邮箱" style="width: 60%">
                    <el-input v-model="editDepartmentForm.mail"></el-input>
                </el-form-item>
                <el-form-item label="描述" style="width: 60%">
                    <el-input v-model="editDepartmentForm.remarks"></el-input>
                </el-form-item>
                <el-form-item label="类型" size="mini" prop="type">
                    <el-radio-group v-model="editDepartmentForm.type">
                        <el-radio :label="1">职能部门</el-radio>
                        <el-radio :label="2">二级院部</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="pushEditDepartment" type="success">保存</el-button>
                <el-button @click="cancelEditDepartment">取 消</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import departmentApi from '../../../api/department';
export default {
    data() {
        return {
            tableData: [],
            addDialogVisible: false,
            addDepartmentForm: {},
            pageInfo: {
                pageNum: 1,
                pageSize: 10,
                total: 0
            },
            editDialogVisible: false,
            editDepartmentForm: {},
            name: ''
        };
    },
    methods: {
        handleSizeChange(val) {
            this.pageInfo.pageSize = val;
            this.getDepartmentData();
        },
        // 分页函数
        handleCurrentChange(current) {
            this.pageInfo.pageNum = current;
            this.getDepartmentData();
        },
        // 查询
        async search() {
            await departmentApi.getByName(this.name).then((res) => {
                this.tableData = res.data;
            });
        },
        addDialog() {
            this.addDepartmentForm = {};
            this.addDialogVisible = true;
        },
        async getDepartmentData() {
            await departmentApi.getAllDepart().then((res) => {
                this.pageInfo.total = res.data.length;
            });
            await departmentApi.getDepartmentByPage(this.pageInfo.pageNum, this.pageInfo.pageSize).then((res) => {
                console.log(res.data);
                this.tableData = res.data;
            });
        },

        // 新增
        async pushAddDepartment(){
            await departmentApi.addDepartment(this.addDepartmentForm).then(res=>{
                console.log(res);
                this.$message({
                    message: '添加部门成功！',
                    type: 'success'
                });
                this.getDepartmentData();
                this.$forceUpdate();
                this.addDialogVisible = false
            });
        },
        async editDepartmentById(id){
            this.editDialogVisible = true;
            await departmentApi.getDepartmentById(id).then(res => {
                console.log(res.data);
                this.editDepartmentForm = res.data;
            });
        },
        // 取消编辑
        cancelEditDepartment() {
            this.editDialogVisible = false;
            this.editDepartmentForm = {};
        },
        // 提交编辑
        async pushEditDepartment(){
            await departmentApi.editDepartment(this.editDepartmentForm.id,this.editDepartmentForm).then(res => {
                console.log(res.data);
                this.$message({
                    message: '修改成功！',
                    type: 'success'
                });
                this.getDepartmentData();
                this.$forceUpdate();
                this.editDialogVisible = false
            });
        },
        async deleteDepartmentById(id){
            const confirmResult = await this.$confirm('此操作将永久删除该项目, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).catch((err) => err);
            if (confirmResult != 'confirm') {
                return;
            }
            if (confirmResult == 'confirm') {
                await departmentApi.deleteDepartment(id).then((res) => {
                    if (res.success == true) {
                        this.$message({
                            message: '删除成功！',
                            type: 'success'
                        });
                    } else {
                        this.$notify.error({
                            title: '操作错误',
                            message: res.message,
                            duration: 0
                        });
                    }
                });
                this.getDepartmentData();
            }
        }
    },
    created() {
        this.getDepartmentData();
    }
};
</script>

<style scoped>
.title {
    border: 1px #bcbcbc;
    width: 50%;
    margin: 0 auto;
}
.card {
    border-radius: 15px;
    width: 50%;
    margin: 0 auto;
}
</style>
